package com.rran.study.chat.ops.service.accept;

import com.rran.study.chat.ops.pojo.socketmsg.ChatMessage;
import com.rran.study.chat.ops.pojo.socketmsg.MessageContext;
import com.rran.study.chat.ops.enums.MessageType;
import com.rran.study.chat.ops.repository.SendHandlerResponsitory;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author yy
 * @Type AbstractAcceptStrategy.java
 * @Desc
 * @date 2020/8/7 10:11
 */
public abstract class AbstractAcceptStrategy<T extends ChatMessage> implements AcceptStrategy, InitializingBean {

    //校验
    protected void validate(MessageContext context) {

    }

    //发送消息
    protected abstract void doAccept(MessageContext<T> context);

    @Override
    public void accept(MessageContext context) {
        validate(context);
        doAccept(context);
    }


    @Override
    public void afterPropertiesSet() {
        AcceptHandler acceptHandler = getClass().getAnnotation(AcceptHandler.class);
        if (acceptHandler == null) {
            throw new RuntimeException("class must be with annotation SendHandler");
        }
        MessageType messageType = acceptHandler.messageType();
        SendHandlerResponsitory.storeSendHandler(messageType.name(), this);
    }
}
